package com.west.common.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.RequiresApi;
import android.support.annotation.StringRes;
import android.support.design.widget.TabLayout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.west.common.common.R;
import com.west.common.uitils.UiUtil;

public class TitleBar extends FrameLayout {

    private TextView mTitle;
    private ImageView mIvTitle;
    private ImageView mIconRight;
    private ImageView mIconLeft;
    private TextView mTvDot;
    private TabLayout mTabLayout;
    private TextView mTvCancle;

    public TitleBar(Context context) {
        super(context);
        init(null, 0, 0);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0, 0);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, defStyleAttr, 0);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs, defStyleAttr, defStyleRes);
    }

    private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        Context context = getContext();

        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.lay_title_bar, this, true);

        mTitle = (TextView) findViewById(R.id.tv_title);
        mIvTitle = (ImageView) findViewById(R.id.iv_title);
        mIconRight = (ImageView) findViewById(R.id.iv_icon_right);
        mIconLeft = (ImageView) findViewById(R.id.iv_icon_left);
        mTvDot = (TextView) findViewById(R.id.home_tv_dot);
        mTabLayout = (TabLayout) findViewById(R.id.layout_tab);
        mTvCancle = (TextView) findViewById(R.id.tv_cancel);

        if (attrs != null) {
            // Load attributes
            final TypedArray a = context.obtainStyledAttributes(
                    attrs, R.styleable.TitleBar, defStyleAttr, defStyleRes);

            String title = a.getString(R.styleable.TitleBar_aTitle);
            Drawable drawableTitle = a.getDrawable(R.styleable.TitleBar_aTitleIcon);
            Drawable drawableRight = a.getDrawable(R.styleable.TitleBar_aRightIcon);
            Drawable drawableLeft = a.getDrawable(R.styleable.TitleBar_aLeftIcon);
            a.recycle();

            if (!TextUtils.isEmpty(title)) {
                mTitle.setText(title);
            } else if (drawableTitle != null) {
                mIvTitle.setImageDrawable(drawableTitle);
            }

            if (drawableLeft != null) {
                mIconLeft.setImageDrawable(drawableLeft);
            }

            if (drawableRight != null) {
                mIconRight.setImageDrawable(drawableRight);
            }
        }

        mTvCancle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setSearchViewVisible(false);
            }
        });

        // Set Background
        setBackgroundColor(getResources().getColor(R.color.titleBg));

        // Init padding
        setPadding(getLeft(), getTop() + UiUtil.getStatusBarHeight(getContext()), getRight(), getBottom());

    }

    public void setTitle(@StringRes int titleRes) {
        if (titleRes == 0){
            return;
        }
        mTitle.setText(titleRes);
    }

    public void setTitle(String value){
        mTitle.setText(value);
    }

    public void setRightTitle(String title){
        if(TextUtils.isEmpty(title)){
            mTvCancle.setVisibility(GONE);
        }else{
            mIconRight.setVisibility(GONE);
            mTvCancle.setVisibility(VISIBLE);
            mTvCancle.setText(title);
        }
    }

    public String getRightTitle(){
        return mTvCancle.getText().toString().trim();
    }

    public void setRightTitleOnClickListener(OnClickListener listener){
        mTvCancle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onClick(v);
                }
            }
        });
    }

    public void setTitleColor(@ColorInt int color){
        mTitle.setTextColor(color);
    }

    public TabLayout getTabLayout() {
        return mTabLayout;
    }

    public void setTitleIcon(@DrawableRes int iconRes) {
        if (iconRes == 0) {
            mIvTitle.setVisibility(GONE);
            return;
        }
        mIvTitle.setImageResource(iconRes);
        mIvTitle.setVisibility(VISIBLE);
    }

    public void setLeftIcon(@DrawableRes int iconRes) {
        if (iconRes == 0) {
            mIconLeft.setVisibility(GONE);
            return;
        }
        mIconLeft.setImageResource(iconRes);
        mIconLeft.setVisibility(VISIBLE);
    }

    public void setTabLayoutVisible(boolean isHide) {
        if (isHide) {
            mTitle.setVisibility(GONE);
            mIvTitle.setVisibility(GONE);
            mIconRight.setVisibility(GONE);
            mTabLayout.setVisibility(GONE);
        } else {
            mTitle.setVisibility(GONE);
            mIvTitle.setVisibility(GONE);
            mIconRight.setVisibility(VISIBLE);
            mTabLayout.setVisibility(VISIBLE);
        }
    }

    public void setUnReadCount(int count){
        if(count <= 0){
            mTvDot.setVisibility(GONE);
        }else{
            mTvDot.setVisibility(VISIBLE);
            //mTvDot.setText(String.valueOf(count));
        }
    }

    public void setRightIcon(@DrawableRes int iconRes) {
        if (iconRes == 0) {
            mIconRight.setVisibility(GONE);
            return;
        }
        mIconRight.setImageResource(iconRes);
        mIconRight.setVisibility(VISIBLE);
    }

    public void setRightOnClickListener(OnClickListener listener) {
        mIconRight.setOnClickListener(listener);
    }

    public void setLeftIconOnClickListener(OnClickListener listener) {
        mIconLeft.setOnClickListener(listener);
    }

    public void setSearchViewVisible(boolean show) {
        if (show) {
            mTitle.setVisibility(GONE);
            mIvTitle.setVisibility(GONE);
            mIconRight.setVisibility(GONE);
            mTvCancle.setVisibility(VISIBLE);
        } else {
            mTitle.setVisibility(VISIBLE);
            mIvTitle.setVisibility(VISIBLE);
            mIconRight.setVisibility(VISIBLE);
            mTvCancle.setVisibility(GONE);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        float d = getResources().getDisplayMetrics().density;
        int minH = (int) (d * 40 + UiUtil.getStatusBarHeight(getContext()));

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(minH, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private OnSearchClickListener mSearchClickListener;

    public void setSearchClickListener(OnSearchClickListener searchClickListener) {
        mSearchClickListener = searchClickListener;
    }

    public interface OnSearchClickListener {
        boolean onQuery(String query);
    }

}
